<template>
  <div>
    <van-list
      class="cmt-list"
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="(item, index) in list" :key="item.com_id">
        <template>
          <div class="user-box">
            <van-image fit="cover" round :src="item.aut_photo" />
            <div class="title">
              <div class="autor">{{ item.aut_name }}</div>
              <div>{{ item.content }}</div>
              <div class="answer">
                <span class="time">{{ item.pubdate | relaTime }}</span>
                <van-button @click="doReply(item)" size="mini"
                  >回复({{ item.reply_count }})</van-button
                >
              </div>
            </div>
            <div @click="lightHeart(index)">
              <van-icon
                name="like"
                size="14"
                color="red"
                v-if="item.is_liking"
              />
              <van-icon name="like-o" size="14" v-else />
            </div>
          </div>
        </template>
      </van-cell>
    </van-list>

    <!-- 输入区域 -->
    <div class="send-box">
      <van-field
        @keyup.enter="send"
        v-model="cmt"
        center
        clearable
        placeholder="请输入评论内容"
      >
        <template #button>
          <van-button @click="send" size="small" type="info">发布</van-button>
        </template>
      </van-field>
    </div>

    <!-- 回复区域 -->
    <van-action-sheet v-model="answerShow" title="发表回复">
      <my-answer :currentCmt="infoAnswer" />
    </van-action-sheet>
  </div>
</template>

<script>
import { cmtListAPI, addCmtListAPI } from '@/api/comment'
import MyAnswer from './MyAnswer.vue'
export default {
  components: {
    MyAnswer
  },
  data() {
    return {
      answerShow: false,
      list: [],
      loading: false,
      finished: false,
      // 跟发表的输入框双向绑定
      cmt: '',
      offset: null,
      infoAnswer: ''
    }
  },
  methods: {
    lightHeart(index) {
      this.list[index].is_liking = !this.list[index].is_liking
    },
    doReply(item) {
      this.answerShow = true
      this.infoAnswer = item
    },
    async onLoad() {
      const res = await cmtListAPI({
        type: 'a',
        source: this.$route.query.id,
        offset: this.offset
      })

      // 把本次返回的last_id作为offset方便下次传递
      this.offset = res.data.last_id
      this.list.push(...res.data.results)

      // 加载完数据把loading改为false
      this.loading = false

      // 如果结束id等于本次的最后一条id，就代表本次请求后加载完了
      if (res.data.end_id == res.data.last_id) {
        this.finished = true
      }
    },

    // 发表评论
    async send() {
      if (this.cmt == '') return this.$toast('内容不能为空')
      const res1 = await addCmtListAPI({
        target: this.$route.query.id,
        content: this.cmt
      })
      this.list.unshift(res1.data.new_obj)
      this.cmt = ''
    }
  }
}
</script>

<style lang="less" scopd>
.van-list {
  margin-bottom: 50px;
  .user-box {
    margin-top: 10px;
    display: flex;
    justify-content: space-between;
    position: relative;
    .title {
      flex: 1;
      .autor {
        font-weight: 600;
      }
      .answer {
        display: flex;
        align-items: center;
        .time {
          color: gray;
          font-size: 12px;
          margin-right: 10px;
        }
      }
    }
    .van-image {
      width: 60px;
      height: 60px;
      margin-right: 15px;
    }
    .heart {
      position: absolute;
      top: 0;
      right: 0;
    }
  }
}
.send-box {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  .van-button--info {
    background-color: #d0355d;
    border: 1px solid #d0355d;
  }
}

.gender-item {
  height: 240px;
}
</style>
